package com.ikas.ai.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * String Util
 *
 * @author HXH
 */
public class StringUtil extends StringUtils {

    private static Logger logger = LoggerFactory.getLogger(StringUtil.class);
    private static String htmlCodeAmp = "&amp;";
    private static String htmlCodeQuot = "&quot;";

    public static final String EMPTY = "";

    /**
     * 格式化输出字符串
     *
     * @param format
     * @param args
     * @return
     */
    public static String format(String format, Object... args) {
        return String.format(format, args);
    }

    public static String toJson(String source) {
        source = source.replace("'", "\\'");
        source = source.replace("\"", "\\\"");
        source = source.replace("\r\n", "\\uoood\\uoooa");
        source = source.replace("\n", "\\uoooa");
        return source;
    }

    public static String join(Object[] obj, String splitString) {
        StringBuilder bld = new StringBuilder();
        String s = "";
        for (Object o : obj) {
            bld.append(splitString);
            bld.append(o.toString());
        }
        s = bld.toString();
        if (s.length() > 0) {
            s = s.substring(1);
        }
        return s;
    }

    public static String subString(String str, int maxLength) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        if (str.length() > maxLength) {
            return str.substring(0, maxLength);
        } else {
            return str;
        }
    }
    public static List<String> replace(List<String> columns) {
        return columns.stream().map(p -> p.replace(".", "_").replace("$", "_"))
                .collect(Collectors.toList());
    }

    public static List<String> rename(List<String> columns) {
        columns = replace(columns);
        Set<String> set = new LinkedHashSet<>();
        for (String str : columns) {
            String value = str;
            for (int i = 1; !set.add(value); i++) {
                value = str.concat("(rename").concat(i + "").concat(")");
            }
        }
        List<String> newList = new ArrayList<>();
        newList.addAll(set);
        return newList;
    }

    public static Number toNumber(String text) {
        try {
            return Byte.parseByte(text);
        } catch (Exception exception) {

        }
        try {
            return Short.parseShort(text);
        } catch (Exception exception) {

        }
        try {
            return Integer.parseInt(text);
        } catch (Exception exception) {

        }
        try {
            return Long.parseLong(text);
        } catch (Exception exception) {

        }
        try {
            return Float.parseFloat(text);
        } catch (Exception exception) {

        }
        try {
            return Double.parseDouble(text);
        } catch (Exception exception) {

        }
        return null;
    }

}
